package top.dancun.eascrawler.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;

import top.dancun.eascrawler.dao.PapersPublishedDao;
import top.dancun.eascrawler.entity.PapersPublishedEntity;
import top.dancun.eascrawler.service.PapersPublishedService;


@Service
public class PapersPublishedServiceImpl extends ServiceImpl<PapersPublishedDao, PapersPublishedEntity> implements PapersPublishedService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<PapersPublishedEntity> page = selectData(params);

        return new PageUtils(page);
    }

    @Override
    public Map<String,Object> echartsData(Map<String, Object> params) {
//        LambdaQueryChainWrapper<PapersPublishedEntity> lambdaQueryWrapper = lambdaQuery();
//        if(Objects.nonNull(params.get("year")) && StringUtils.isNotBlank(params.get("year").toString())){
//            lambdaQueryWrapper.eq(PapersPublishedEntity::getYear,params.get("year"));
//        }
//        if(Objects.nonNull(params.get("from")) && StringUtils.isNotBlank(params.get("from").toString())){
//            lambdaQueryWrapper.eq(PapersPublishedEntity::getSchoolname,params.get("from"));
//        }
//        List<PapersPublishedEntity> list = new ArrayList<>();
//        if(Objects.nonNull(params.get("order")) && StringUtils.isNotBlank(params.get("order").toString())){
//           String order = params.get("order").toString();
//           boolean isAsc = Boolean.parseBoolean(params.get("isAsc").toString());
//           if(order.equals("num")){
//               lambdaQueryWrapper.orderBy(true,isAsc,PapersPublishedEntity::getNum);
//           }else if(order.equals("ch")){
//               lambdaQueryWrapper.orderBy(true,isAsc,PapersPublishedEntity::getChNum);
//           } else if (order.equals("en")) {
//               lambdaQueryWrapper.orderBy(true,isAsc,PapersPublishedEntity::getEnNum);
//           }
//            list = lambdaQueryWrapper.page(new Page<>(1,10)).getRecords();
//            return echartsDataUtils(list,order);
//        }
//        list = lambdaQueryWrapper.page(new Page<>(1,10)).getRecords();
        params.put("page",1);
        params.put("limit",20);
        List<PapersPublishedEntity> list = selectData(params).getRecords();
        Object orderObj = params.get("order");
        return echartsDataUtils(list,orderObj==null?"num":orderObj.toString());

    }

    /**
     * 将列表转换为echarts需要的数据格式
     * @param list
     * @param order
     * @return
     */
    private Map<String,Object> echartsDataUtils(List<PapersPublishedEntity> list,String order){
        Map<String,Object> data = new HashMap<>();
        //echart图的高校名
        List<String> yAxisData = new ArrayList<>();
        //echart图的series数据
        List<Object> series = new ArrayList<>();
        //中文文献数量列表
        List<Long> chData = new ArrayList<>();
        //英文文献数量列表
        List<Long> enData = new ArrayList<>();
        //文献总数数量列表
        List<Long> numData = new ArrayList<>();

        list.forEach(papersPublishedEntity -> {
            yAxisData.add(papersPublishedEntity.getSchoolname());
            enData.add(papersPublishedEntity.getEnNum());
            chData.add(papersPublishedEntity.getChNum());
            numData.add(papersPublishedEntity.getNum());
        });
        if(order.equals("num")){
            Map<String,Object> seriesItem = new HashMap<>();
            seriesItem.put("name","文献总数");
            seriesItem.put("type","bar");
            seriesItem.put("data",numData);
            series.add(seriesItem);
        }
        if(order.equals("chNum") || order.equals("num")){
            Map<String,Object> seriesChItem = new HashMap<>();
            seriesChItem.put("name","中文文献");
            seriesChItem.put("type","bar");
            seriesChItem.put("data",chData);
            series.add(seriesChItem);
        }
        if (order.equals("enNum") || order.equals("num")) {
            Map<String,Object> seriesEnItem = new HashMap<>();
            seriesEnItem.put("name","英文文献");
            seriesEnItem.put("type","bar");
            seriesEnItem.put("data",enData);
            series.add(seriesEnItem);
        }
        data.put("yAxisData",yAxisData);
        data.put("series",series);

        return data;
    }

    private IPage<PapersPublishedEntity> selectData (Map<String,Object> params){
        LambdaQueryChainWrapper<PapersPublishedEntity> lambdaQueryWrapper = lambdaQuery();
        if(Objects.nonNull(params.get("year")) && StringUtils.isNotBlank(params.get("year").toString())){
            lambdaQueryWrapper.eq(PapersPublishedEntity::getYear,params.get("year"));
        }
        if(Objects.nonNull(params.get("from")) && StringUtils.isNotBlank(params.get("from").toString())){
            lambdaQueryWrapper.like(PapersPublishedEntity::getSchoolname,params.get("from"));
        }
       IPage page = null;
        if(Objects.nonNull(params.get("order")) && StringUtils.isNotBlank(params.get("order").toString())){
            String order = params.get("order").toString();
            boolean isAsc = Boolean.parseBoolean(params.get("isAsc").toString());
            if(order.equals("num")){
                lambdaQueryWrapper.orderBy(true,isAsc,PapersPublishedEntity::getNum);
            }else if(order.equals("chNum")){
                lambdaQueryWrapper.orderBy(true,isAsc,PapersPublishedEntity::getChNum);
            } else if (order.equals("enNum")) {
                lambdaQueryWrapper.orderBy(true,isAsc,PapersPublishedEntity::getEnNum);
            }

        }else{
            lambdaQueryWrapper.orderByDesc(PapersPublishedEntity::getNum);
        }
        page =  lambdaQueryWrapper.page(new Query<PapersPublishedEntity>().getPage(params));
        return page;
    }


}